package com.epcs.controller.action;

import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.epcs.model.bo.EventcategoryBO;
import com.epcs.model.dto.Account;
import com.epcs.model.dto.Eventcategory;
import com.epcs.model.dto.FollowingCategory;
import com.epcs.model.dto.MostFollowingList;
import com.opensymphony.xwork2.ActionSupport;

public class ManageCategoryAction extends ActionSupport implements
		SessionAware, ServletRequestAware {
	private SessionMap<String, Object> sessionMap;
	private HttpServletRequest request;
	private String categoryName;
	private String isActive;
	private String categoryIDForActive;
	private InputStream inputStream;
	private String NameAjax;
	private String idForUpdate;
	private String nameForUpdate;
	
	

	

	public String showCategoryList() {
		EventcategoryBO eventcategoryBO = new EventcategoryBO();
		Account account = (Account) sessionMap.get("USER_INFO");
		if (account != null) {
			if ("Admin".equals(account.getRole().getRoleName())) {
				List<Eventcategory> eventcategories = eventcategoryBO
						.getAllEventCategory(true);
				if (eventcategories != null) {
					
					List<MostFollowingList> FollowingCategoryList = eventcategoryBO.getAllFollowingCategory();
					List<FollowingCategory> list = new ArrayList<FollowingCategory>();
					for(int i = 0 ; i < eventcategories.size(); i++){
						FollowingCategory followingCategory = new FollowingCategory();
						followingCategory.setEventCategoryId(eventcategories.get(i).getEventCategoryId());
						followingCategory.setEventCategoryName(eventcategories.get(i).getEventCategoryName());
						followingCategory.setIsActive(eventcategories.get(i).getIsActive());
						if(FollowingCategoryList!=null){
							for(int j = 0 ; j < FollowingCategoryList.size() ; j++){
								if(eventcategories.get(i).getEventCategoryId() == FollowingCategoryList.get(j).getIdOfFollow()){
									followingCategory.setCountById(FollowingCategoryList.get(j).getCountById());
								}
							}
						}else{
							followingCategory.setCountById(0);
						}
						
						list.add(followingCategory);
					}
					request.setAttribute("LIST_CATEGORY", list);
				} else {
					request.setAttribute("LIST_CATEGORY", null);
				}
				return "Success";
			} else {
				return "Unauthorized";
			}
		} else {
			return "Unauthorized";
		}

	}
	
	public String createCategory(){
		EventcategoryBO eventcategoryBO = new EventcategoryBO();
		Account account = (Account) sessionMap.get("USER_INFO");
		if (account != null) {
			if ("Admin".equals(account.getRole().getRoleName())) {
				// kiểm tra xem evencategory đã tồn tại hay chưa
				Eventcategory eventcategory =  eventcategoryBO.getEventCategory(categoryName);
				if(eventcategory != null){
					request.setAttribute("IS_EXIST", true);
				}else {
					Eventcategory newCategory =  new Eventcategory();
					newCategory.setEventCategoryName(categoryName);
					newCategory.setIsActive(true);
					boolean check = eventcategoryBO.insertCategoryName(newCategory);
				}
				return "Success";
			} else {
				return "Unauthorized";
			}
		} else {
			return "Unauthorized";
		}
	}

	public String activeCategory(){
		EventcategoryBO eventcategoryBO = new EventcategoryBO();
		Account account = (Account) sessionMap.get("USER_INFO");
		if (account != null) {
			if ("Admin".equals(account.getRole().getRoleName())) {
				
				if("true".equals(isActive)){
					Eventcategory eventcategory = eventcategoryBO.getCategoryByID(categoryIDForActive);
					eventcategory.setIsActive(false);
					eventcategoryBO.updateCategory(eventcategory);
				}else{
					Eventcategory eventcategory = eventcategoryBO.getCategoryByID(categoryIDForActive);
					eventcategory.setIsActive(true);
					eventcategoryBO.updateCategory(eventcategory);
				}
			
				return "Success";
			} else {
				return "Unauthorized";
			}
		} else {
			return "Unauthorized";
		}
	}
	
	public String checkIsExist(){
		EventcategoryBO eventcategoryBO = new EventcategoryBO();
		Eventcategory eventcategory =  eventcategoryBO.getEventCategory(NameAjax);
		if(eventcategory !=null){
			inputStream = new StringBufferInputStream("true");
		}else{
			inputStream = new StringBufferInputStream("false");
		}
	
		return SUCCESS;
	}
	public String updateCategory(){
		EventcategoryBO eventcategoryBO = new EventcategoryBO();
		Eventcategory check = eventcategoryBO.getEventCategory(nameForUpdate);
		if(check ==null){
			Eventcategory category = eventcategoryBO.getCatByID(Integer.valueOf(idForUpdate));
			category.setEventCategoryName(nameForUpdate.trim());
			boolean isSuccess = eventcategoryBO.updateCategory(category); 
			if(isSuccess){
				inputStream = new StringBufferInputStream(nameForUpdate.trim());
			} else{
				inputStream = new StringBufferInputStream("fasle");
			}
		}
		else{
			inputStream = new StringBufferInputStream("true");
		}
		
		return SUCCESS;
	}

	
	//----------------Getter and Setter---------------------------
	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}
	public String getIsActive() {
		return isActive;
	}

	public void setIsActive(String isActive) {
		this.isActive = isActive;
	}

	public String getCategoryIDForActive() {
		return categoryIDForActive;
	}

	public void setCategoryIDForActive(String categoryIDForActive) {
		this.categoryIDForActive = categoryIDForActive;
	}
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getNameAjax() {
		return NameAjax;
	}

	public void setNameAjax(String nameAjax) {
		NameAjax = nameAjax;
	}
	public String getIdForUpdate() {
		return idForUpdate;
	}

	public void setIdForUpdate(String idForUpdate) {
		this.idForUpdate = idForUpdate;
	}

	public String getNameForUpdate() {
		return nameForUpdate;
	}

	public void setNameForUpdate(String nameForUpdate) {
		this.nameForUpdate = nameForUpdate;
	}
	// ---------------- session --------------------//

	

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = (SessionMap<String, Object>) sessionMap;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
}
